<?php

namespace catchAdmin\info\controller;

use app\Request;
use catchAdmin\permissions\model\Users;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;
use think\facade\Db;


class Jm extends catchController
{
    public function generateUUID()
    {
        $data = random_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // 设置版本为4
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // 设置变体为RFC 4122

        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        $role = Db::name('user_has_roles')->where('uid', $user->id)->value('role_id');
        $page = request()->param();



        if ($role == 8) {
            $data = Db::name('jm')
                ->where('creator_id', $user->id)
                ->when(!empty($page['jmj']), function($query) use ($page) {
                    $query->whereLike('field101', $page['jmj']);
                })
                ->when(!empty($page['jm']), function($query) use ($page) {
                    $query->whereLike('name', $page['jm']);
                })->order('id', 'desc')
                ->paginate([
                    'list_rows' => $page['size'],
                    'page' => $page['page'],
                ])->toArray();
        } else {
            $data = Db::name('jm')
                ->when(!empty($page['jmj']), function($query) use ($page) {
                    $query->whereLike('field101', $page['jmj']);
                })
                ->when(!empty($page['jm']), function($query) use ($page) {
                    $query->whereLike('name', $page['jm']);
                })->order('id', 'desc')
                ->paginate([
                    'list_rows' => $page['size'],
                    'page' => $page['page'],
                ])->toArray();;
        }


        foreach ($data['data'] as &$v) {
            $v['a5'] = Users::where('id', $v['creator_id'])->find()['username'];
            $v['a6'] =   $auth->user()->id;
        }

        return CatchResponse::success($data, '提交成功', 10000);

    }

    public function add(CatchAuth $auth)
    {
        $user = $auth->user();
        $data = request()->param();

        $data['created_at'] = date('Y-m-d H:i:s');
        $data['shenhe'] = 0;
        $data['fasong'] = 0;

//        p($data);

        if (is_int($data['field101'])){
            $data['uid'] =$data['field101'];
            $data['field101'] = Db::name('jmj')->where('id', $data['field101'])->value('name');
        }


        if (!empty($data['id'])){
            $data['uid'] = Db::name('jmj')->where('name', $data['field101'])->value('id');
            $data['creator_id'] = db::name('jm')->find($data['id'])['creator_id'];
//            $data['uuid']= db::name('jm')->find($data['id'])['uuid'];
        } else {
            $data['creator_id'] = $user->id;

        }
        $data['uuid'] = $this->generateUUID();
        Db::name('jm')->save($data);
        return CatchResponse::success(1, '提交成功', 10000);



    }

    public function del()
    {
        Db::name('jm')->where('id', request()->param('id'))->delete();
        return CatchResponse::success();
    }


    public function getinfo()
    {

        $data = [];

        $data['s1'] = Db::name('jmj')->count();  //节目集总数
        $data['s2'] = Db::name('jm')->count();   //节目总数

        $data['s3'] = Db::name('user_has_roles')->where('role_id', 8)->count();
        $data['s4'] = Db::name('user_has_roles')->where('role_id', 9)->count();


        $users = Db::name('user_has_roles')->where('role_id', 8)->field('uid')->select(

        )->toArray();
        if (request()->param('b') != "undefined" || request()->param('v') != "undefined"){
            foreach ($users as &$v){

                $v['jmj'] = Db::name('jmj')->where('creator_id', $v['uid'])->whereBetween('created_at', [request()->param('b'), request()->param('v')])->count();
                $v['jm'] = Db::name('jm')->where('creator_id', $v['uid'])->whereBetween('created_at', [request()->param('b'), request()->param('v')])->count();
                $v['name'] = Db::name('users')->where('id', $v['uid'])->value('username');

            }
        } else {
            foreach ($users as &$v){

                $v['jmj'] = Db::name('jmj')->where('creator_id', $v['uid'])->count();
                $v['jm'] = Db::name('jm')->where('creator_id', $v['uid'])->count();
                $v['name'] = Db::name('users')->where('id', $v['uid'])->value('username');

            }
        }
        $data['s5'] = $users;
        return CatchResponse::success($data, 0, 10000);

    }
}